package org.pragma.objects;

import java.util.ArrayList;
import java.util.List;

import org.pragma.PragmaClass;
import org.pragma.PragmaContext;
import org.pragma.PragmaBlock;

public class PragmaObject {

	PragmaClass classObject;
	List<PragmaObject> referers = new ArrayList<PragmaObject>();
		PragmaContext objectContext;
	Object value;
	public final static String displayIndent = "  ";
	
	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public PragmaObject(){
		//objectContext = new PragmaContext();
	}
	
	public PragmaClass getClassObject() {
		return classObject;
	}

	public PragmaObject execute(PragmaContext ctx,String methodName,List<PragmaObject> parameters) throws Exception{
		PragmaBlock method = classObject.getMethod(methodName);		
		if(method==null){
			//System.out.println("can't found method:"+classObject.getName()+"."+methodName);
			throw new Exception("can't found method:"+classObject.getName()+"."+methodName);
		}
		ctx.set("this",this);
		return method.execute(ctx,parameters);		
	}

	public PragmaObject execute(PragmaContext ctx) throws Exception {
		return this;
	}

	public PragmaObject setClassObject(PragmaClass classObject) {
		this.classObject = classObject;
		return this;
	}
	
	@Override
	public String toString() {
		return super.hashCode()+":"+(classObject==null?"_":classObject.toString());
	}

	public PragmaContext getContext() {
		if(objectContext==null){
			objectContext = new PragmaContext();
			objectContext.setCtxTitle(classObject.getName()+"Context");
		}
		return objectContext;
	}
	
	public String display(String margin){
		return margin+(this.classObject==null?"":this.classObject.getName())
			+":"+
		(value==null?(objectContext==null?"":objectContext.display("")):value.toString());
	}
	
	public List<PragmaObject> getReferers() {
		return referers;
	}

}
